using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Perovich.GameObjects.Interfaces;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Perovich.GameObjects.Helpers;

namespace Perovich.GameObjects.Controls
{

    /// <summary>
    /// The <see cref="Screen"/> class is a <see cref="Control"/> that draws all of its children conponents in one batch on the screen.
    /// </summary>
    public class Screen : Control
    {
        /// <summary>
        /// Constructs a new instance of the <see cref="Screen"/> class.
        /// </summary>
        /// <param name="game">
        ///     <see cref="Perovich.GameObjects.Interfaces.ICanHoldComponents"/> instance that this <see cref="GameObject"/> will be constructed for.
        /// </param>
        public Screen(ICanHoldComponents game)
            : base(game)
        {
            DrawScreen = true;
        }



        /// <summary>
        /// Loads the graphics content for this <see cref="Screen"/>.
        /// </summary>
        protected override void LoadContent()
        {
            base.LoadContent();
        }

        /// <summary>
        /// Deriving classes can override this method to draw on the screen.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of the game's timing values.</param>
        protected virtual void DrawOnscreen(GameTime gameTime){ }


        /// <summary>
        /// Allows the <see cref="Screen"/> to draw on its render target.
        /// </summary>
        /// <param name="gameTime">A snapshot of the games timing values.</param>
        public override void DrawOnTargets(GameTime gameTime)
        {
            base.DrawOnTargets(gameTime);
            if (DrawScreen)
            {
                RenderTarget2D t = null;
                t = RenderTargetManager.GetRenderTarget(GraphicsDevice, Width, Height, this);
                GraphicsDevice.SetRenderTarget(t);
                GraphicsDevice.Clear(BackgroundColor);
                //SBatches.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
                DrawOnscreen(gameTime);
                base.Draw(gameTime);
                //SBatches.End();
                GraphicsDevice.SetRenderTarget(null);
            }
        }


        /// <summary>
        /// Allows the <see cref="Screen"/> to draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of the game's timing values.</param>
        /// <remarks>Classes extending <see cref="Screen"/> should not override this method. Override <see cref="Screen.DrawOnscreen"/> instead.</remarks>
        public override void Draw(GameTime gameTime)
        {
            if (DrawScreen)
            {
                SBatches.CurrentBatch.Draw(RenderTargetManager.GetRenderTarget(GraphicsDevice, Width, Height, this), Position, new Rectangle(0, 0, (int)Width, (int)Height), Color.White);
            }
            else
            {
                base.Draw(gameTime);
            }
        }

        /// <summary>
        /// Deriving classes can set this to false to inhibit the <see cref="Screen"/>'s normal draw behavior, i.e. to implement new behavior.
        /// </summary>
        protected bool DrawScreen { get; set; }
    }
}
